Thực đơn
Object_Pascal Object Pascal của Apple=program ObjectPascalExample; type THelloWorld = object procedure Put; end; var HelloWorld: THelloWorld; procedure THelloWorld.Put; begin ShowMessage('Hello, World!'); end;begin New(HelloWorld); HelloWorld.Put; Dispose(HelloWorld);end.
Vẫn được hỗ trợ trong Delphi và Free Pascal. FPC cũng đóng gói các thư viện/đơn vị của riêng nói. Nhưng Delphi thì không. Free Pascal 2.0 được viết lại theo phương ngữ giống Delphi hơn.
program ObjectPascalExample; type THelloWorld = object procedure Put; end; procedure THelloWorld.Put; begin WriteLn('Hello, World!'); end;var HelloWorld: THelloWorld; { allocated on the stack and can be used without explicit allocation. }begin HelloWorld.Put;end.
program ObjectPascalExample; type PHelloWorld = ^THelloWorld; THelloWorld = object procedure Put; end; procedure THelloWorld.Put; begin WriteLn('Hello, World!'); end;var HelloWorld: PHelloWorld; { this is a typed pointer to a THelloWorld }begin New(HelloWorld); HelloWorld^.Put; Dispose(HelloWorld);end.
Ví dụ khác:
program ObjectPascalExample; type PHelloWorld = ^THelloWorld; THelloWorld = object procedure Put; end; procedure THelloWorld.Put; begin WriteLn('Hello, World!'); end;var HelloWorld: PHelloWorld; { this is a typed pointer to a THelloWorld } HelloWorld2: ^THelloWorld; { this is a direct variable to a pointer of the THelloWorld type. This variable is type incompatible with PHelloWorld. } HelloWorld3: ^THelloWorld; { the compiler sees this as being type incompatible with HelloWorld2 *and* PHelloWorld. Using the pointer syntax is the only way to declare a type that is assignment compatible. } HelloWorld4: PHelloWorld; { this is type compatible with HelloWorld, but incompatible with the other two variables. }begin { This works in a similar way as the code above... but it demonstrates the disadvantage of not using a specific type. The type definition between two variables using the raw pointer based notation would be seen as "different" by the compiler. } New(HelloWorld4); HelloWorld:= HelloWorld4; { this is valid - the type assignment is valid } HelloWorld2 = HelloWorld; { this would be a compiler error } {disposes left out for brevity} New(HelloWorld3); HelloWorld:= HelloWorld3; { this would be a compiler error } HelloWorld2 = HelloWorld3; { this would be a compiler error } {disposes left out for brevity}end.
Đây là một khác biệt quan trọng để tạo ra, và có thể được nhìn nhận là một trong những khác biệt chính dẫn đến phong cách của Delphi để "ẩn" cấp phát heap và kí hiệu con trỏ từ lập trình viên. Delphi đã loại bỏ sự cần thiết của kiểu con trỏ và cú pháp con trỏ bổ sung, nhưng vẫn giữa lại cơ chế xây dựng rõ ràng.
program ObjectPascalExample;type THelloWorld = class procedure Put; end;procedure THelloWorld.Put;begin Writeln('Hello, World!');end;var HelloWorld: THelloWorld; { this is an implicit pointer }begin HelloWorld:= THelloWorld.Create; { constructor returns a pointer to an object of type THelloWorld } HelloWorld.Put; HelloWorld.Free; { this line deallocates the THelloWorld object pointed to by HelloWorld }end.
Lưu ý rằng xây dựng đối tượng vẫn có sẵn trong Delphi và Free Pascal.
namespace ObjectPascalExample; interface type ConsoleApp = class class method Main; end; THelloWorld = class method Put; end; implementation method THelloWorld.Put; begin Console.WriteLine('Hello, World!'); end; class method ConsoleApp.Main; begin var HelloWorld:= new THelloWorld; HelloWorld.Put; end;end.
type THelloWorld = class procedure Put; begin PrintLn('Hello, World!'); end end;var HelloWorld:= THelloWorld.Create;HelloWorld.Put;
Lưu ý rằng hiện thực phương thức có thể được thực hiện ở vị trí riêng biệt như trong các phương ngữ Object Pascal khác.
Thực đơn
Object_Pascal Object Pascal của Apple=Liên quan
Object Pascal Objective-C Objection (Tango) Object Linking and Embedding Object-oriented programming Object lifetime Obreck Obrechies Objezierze, Tỉnh West Pomeranian ObleckTài liệu tham khảo
WikiPedia: Object_Pascal http://dn.codegear.com/article/34324 http://edn.embarcadero.com/article/38132 http://code.google.com/p/dwscript/ http://www.mactech.com/articles/mactech/Vol.02/02.... http://www.remobjects.com/ps http://www.roblocher.com/whitepapers/opascal.html http://turbo51.com http://www.delphibasics.info http://www.delphiprogramming.org http://wiki.freepascal.org/iPhone/iPod_development